package com.hilaver.dzmis.web.servlet.basicinformation;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Blob;
import java.util.List;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.hibernate.Hibernate;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.basicinfo.BiAccessoryOthers;
import com.hilaver.dzmis.basicinfo.BiFactory;
import com.hilaver.dzmis.basicinfo.BiFranceCustomer;
import com.hilaver.dzmis.service.impl.BiAccessoryOthersServiceImpl;
import com.hilaver.dzmis.service.impl.BiFranceCustomerServiceImpl;
import com.hilaver.dzmis.util.StringUtils;
import com.hilaver.dzmis.web.servlet.AbstractBaseServlet;

public class BiAccessoryOthersServlet extends AbstractBaseServlet {

	private static final long serialVersionUID = -8209339914606360762L;
	
	public final static int ACTION_GET_MAX_APPLICATION_REFERENCE_INDEX = 20;
	public final static int ACTION_GET_MAX_ACCESSORY_REFERENCE_INDEX = 21;
	
	private BiAccessoryOthers biAO;

	private BiAccessoryOthersServiceImpl aoService;

	public BiAccessoryOthersServlet() {
		super();
		this.aoService = new BiAccessoryOthersServiceImpl();
		this.aoService.setRealFileDir(UPLOAD_TEMP_REAL_DIR);
		this.simpleName = BiAccessoryOthers.class.getSimpleName();
		this.fullName = BiAccessoryOthers.class.getName();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter writer = response.getWriter();
		try {
			this.aoService.setLocale((Locale) request.getSession()
					.getAttribute(Constants.SESSION_LOCALE));
			String outputStr = execute(request);
			if (this.action == ACTION_EDIT) {
				response.setContentType(Constants.HTML_RESPONSE_TYPE);
			}
			writer.write(outputStr);
		} catch (Exception e) {
			writer.print(e.getMessage());
			e.printStackTrace();
		}
		writer.close();
	}

	protected String execute(HttpServletRequest request) throws Exception {
		super.setAction(request);
		super.setAcUser(request, this.aoService);
		String rtn = "";
		switch (this.action) {
			case ACTION_GET: // "get one"
				super.setId(request);
				rtn = this.aoService.get(this.id);
				break;
			case ACTION_GET_WITH_DESCRIPTION:
				super.setId(request);
				rtn = this.aoService.getWithDescription(this.id);
				break;
			case ACTION_DELETE:
				super.setId(request);
				rtn = this.aoService.delete(this.id);
				break;
			case ACTION_GET_ALL_PAGINATION_FILTER:
				super.setPaginationParam(request);
				super.setFilterParam(request);
				rtn = this.aoService.getAllPagination(this.page, this.sort,
						this.dir, super.filters);
				break;
			case ACTION_EDIT:
				this.fillObj(request);
				rtn = this.aoService.edit(this.biAO);
				break;
			case ACTION_GET_MAX_APPLICATION_REFERENCE_INDEX:
				rtn = this.aoService.getMaxApplicationReferenceIndex();
				break;
			case ACTION_GET_MAX_ACCESSORY_REFERENCE_INDEX:
				String ctAccessoryType = (String)this.getValue(request, "ctAccessoryType");
				rtn = this.aoService.getMaxAccessoryReferenceIndex(ctAccessoryType);
				break;
		}
		return rtn;
	}
	
	private void fillObj(HttpServletRequest request) throws Exception{
		super.fillObj(request);
		this.biAO = (BiAccessoryOthers)this.obj;
		
		String fcId = (String) getValue(request, "customerId");
		if (!StringUtils.isEmpty(fcId)) {
			BiFranceCustomer biFc = new BiFranceCustomer();
			biFc.setId(Integer.parseInt(fcId));
			this.biAO.setBiFranceCustomer(biFc);
		}

		String fId = (String) getValue(request, "factoryId");
		if (!StringUtils.isEmpty(fId)) {
			BiFactory biF = new BiFactory();
			biF.setId(Integer.parseInt(fId));
			this.biAO.setBiFactory(biF);
		}
		
	}

}
